#include "TemplateMatch.h"

void CalNumCharacters(RandomVar* var, float Simple, float times)
{
    if (times == 1)
    {
        var->Mean = Simple;
        var->Variance = 0.0f;
        return;
    }

    float NowMaen = (times - 1.0f) * var->Mean / times + Simple / times;
    float NowVariance = (times - 2.0f) * var->Variance / (times - 1.0f)
        + Simple * Simple / (times - 1.0f)
        - times * NowMaen * NowMaen / (times - 1.0f)
        + var->Mean * var->Mean;

    var->Mean = NowMaen;
    var->Variance = NowVariance;
}

int SingleVarMacth(const RandomVar* var, float Simple)
{
    float StandardDev = sqrt(var->Variance);
    return (Simple < var->Mean + 3.0f * StandardDev) && (Simple > var->Mean - 3.0f * StandardDev);
}

int DoubleVarMacth(const RandomVar* var1, const RandomVar* var2, float Simple1, float Simple2)
{
    float tmp1 = (var1->Mean - Simple1) * (var1->Mean - Simple1) / (9.0f * var1->Variance);
    float tmp2 = (var2->Mean - Simple2) * (var2->Mean - Simple2) / (9.0f * var2->Variance);
    return tmp1 + tmp2 < 1.0f;
}
